package com.ruoyi.cs.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.cs.domain.TGoods;
import com.ruoyi.cs.domain.TUser;
import com.ruoyi.cs.domain.TUserRank;
import com.ruoyi.cs.domain.vo.TGoodsUpgrateRecentlyVo;
import com.ruoyi.cs.mapper.TGoodsMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.TGoodsUpgrateMapper;
import com.ruoyi.cs.domain.TGoodsUpgrate;
import com.ruoyi.cs.service.ITGoodsUpgrateService;

/**
 * 幸运饰品升级记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@Service
@SuppressWarnings("all")
public class TGoodsUpgrateServiceImpl implements ITGoodsUpgrateService 
{
    @Autowired
    private TGoodsUpgrateMapper tGoodsUpgrateMapper;
    @Autowired
    private TGoodsMapper tGoodsMapper;

    /**
     * 查询幸运饰品升级记录
     * 
     * @param luckyId 幸运饰品升级记录ID
     * @return 幸运饰品升级记录
     */
    @Override
    public Response<Map<String,Object>> selectTGoodsUpgrateById(String luckyId)
    {
        TGoodsUpgrate  tGoodsUpgrate = tGoodsUpgrateMapper.selectTGoodsUpgrateById(luckyId);
        TGoods lucky = null;
        if (StringUtils.isNotEmpty(tGoodsUpgrate.getLuckyId())) {
            lucky = tGoodsMapper.selectTGoodsById(tGoodsUpgrate.getLuckyId());
        }
        TGoods give = tGoodsMapper.selectTGoodsById(tGoodsUpgrate.getGiveGoodsId());
        Map<String,Object> map = new HashMap<>();
        map.put("luckyTGoods",lucky);
        map.put("giveTGoods",give);
        return Response.success(map);
    }

    /**
     * 查询幸运饰品升级记录列表
     * 
     * @param tGoodsUpgrate 幸运饰品升级记录
     * @return 幸运饰品升级记录
     */
    @Override
    public List<TGoodsUpgrate> selectTGoodsUpgrateList(TGoodsUpgrate tGoodsUpgrate)
    {
        return tGoodsUpgrateMapper.listBack(tGoodsUpgrate);
    }

    /**
     * 新增幸运饰品升级记录
     * 
     * @param tGoodsUpgrate 幸运饰品升级记录
     * @return 结果
     */
    @Override
    public int insertTGoodsUpgrate(TGoodsUpgrate tGoodsUpgrate)
    {
        tGoodsUpgrate.setCreateTime(DateUtils.getNowDate());
        return tGoodsUpgrateMapper.insertTGoodsUpgrate(tGoodsUpgrate);
    }

    /**
     * 修改幸运饰品升级记录
     * 
     * @param tGoodsUpgrate 幸运饰品升级记录
     * @return 结果
     */
    @Override
    public int updateTGoodsUpgrate(TGoodsUpgrate tGoodsUpgrate)
    {
        tGoodsUpgrate.setUpdateTime(DateUtils.getNowDate());
        return tGoodsUpgrateMapper.updateTGoodsUpgrate(tGoodsUpgrate);
    }

    /**
     * 批量删除幸运饰品升级记录
     * 
     * @param luckyIds 需要删除的幸运饰品升级记录ID
     * @return 结果
     */
    @Override
    public int deleteTGoodsUpgrateByIds(String[] luckyIds)
    {
        return tGoodsUpgrateMapper.deleteTGoodsUpgrateByIds(luckyIds);
    }

    /**
     * 删除幸运饰品升级记录信息
     * 
     * @param luckyId 幸运饰品升级记录ID
     * @return 结果
     */
    @Override
    public int deleteTGoodsUpgrateById(String luckyId)
    {
        return tGoodsUpgrateMapper.deleteTGoodsUpgrateById(luckyId);
    }

    @Override
    public List<TGoodsUpgrateRecentlyVo> recentlyOpen(String goodsId) {
        return tGoodsUpgrateMapper.recentlyOpen(goodsId);
    }

    /**
     * 幸运饰品排名
     * @param type
     * @return
     */
    @Override
    public List<TGoodsUpgrate> upgrateRanking(String type) {
        if(type.equalsIgnoreCase("today")){
            type = "0";
        }else {
            type = "1";
        }
        List<TGoodsUpgrate> result = new ArrayList<>();
        List<TGoodsUpgrate> tGoodsUpgrateList = tGoodsUpgrateMapper.upgrateRanking(type);
        if(CollectionUtils.isNotEmpty(tGoodsUpgrateList)){
            Map<String, TGoodsUpgrate> collect1 = tGoodsUpgrateList.stream().collect(Collectors.toMap(TGoodsUpgrate::getUserId, item -> item, (v1, v2) -> v1.getGoodsPrice().compareTo(v2.getGoodsPrice()) > 0 ? v1 : v2));
            List<TGoodsUpgrate> upgrateList = collect1.values().stream().sorted(Comparator.comparing(TGoodsUpgrate::getGoodsPrice).reversed()).collect(Collectors.toList());
            int num = upgrateList.size() > 5 ? 5 : upgrateList.size();
            result = upgrateList.subList(0,num);
            for (TGoodsUpgrate tGoodsUpgrate : result) {
                tGoodsUpgrate.setUserId(null);
            }
        }
        return result;
    }

}
